package com.hupu.jrs.ui.messagelist

import android.app.Fragment
import android.app.FragmentManager
import android.content.Context
import android.content.Intent
import android.support.design.widget.AppBarLayout
import android.support.design.widget.TabLayout
import android.support.v13.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.widget.Toolbar
import java.util.ArrayList
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.R
import com.hupu.jrs.injector.HasComponent
import com.hupu.jrs.ui.BaseSwipeBackActivity
import com.hupu.jrs.ui.pmlist.PmListFragment

/**
 * Created by sll on 2016/3/13.
 */
class MessageActivity : BaseSwipeBackActivity(), HasComponent<MessageComponent> {

    @BindView(R.id.toolbar)
    internal var toolbar: Toolbar? = null
    @BindView(R.id.tabs)
    internal var tabs: TabLayout? = null
    @BindView(R.id.appbar)
    internal var appbar: AppBarLayout? = null
    @BindView(R.id.viewpager)
    internal var viewpager: ViewPager? = null

    var component: MessageComponent? = null
        private set

    protected override val isApplyStatusBarTranslucency: Boolean
        get() = true

    protected override val isApplyStatusBarColor: Boolean
        get() = true

    override fun initContentView(): Int {
        return R.layout.activity_message
    }

    override fun initInjector() {
        component = DaggerMessageComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule())
                .build()
    }

    override fun initUiAndListener() {
        ButterKnife.bind(this)
        initToolBar(toolbar)
        setTitle("消息中心")
        val adapter = MyAdapter(getFragmentManager())
        adapter.addFragment(MessageListFragment(), "回复")
        adapter.addFragment(PmListFragment(), "PM")
        viewpager!!.adapter = adapter
        tabs!!.setupWithViewPager(viewpager)
    }

    class MyAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        private val mFragments = ArrayList<Fragment>()
        private val mFragmentTitles = ArrayList<String>()

        fun addFragment(fragment: Fragment, title: String) {
            mFragments.add(fragment)
            mFragmentTitles.add(title)
        }

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }

        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mFragmentTitles[position]
        }
    }

    companion object {

        fun startActivity(mContext: Context) {
            val intent = Intent(mContext, MessageActivity::class.java)
            mContext.startActivity(intent)
        }
    }
}
